package com.qf.business.course.controller;

import com.qf.business.course.service.CourseDescInfoService;
import com.qf.business.course.service.CourseInfoService;
import com.qf.business.course.service.UserBuyRecoredService;
import com.qf.commons.data.base.BaseUser;
import com.qf.commons.data.result.R;
import com.qf.commons.web.aspect.annotation.GetUser;
import com.qf.commons.web.utils.UserUtils;
import com.qf.data.course.entity.CourseDescInfo;
import com.qf.data.course.entity.CourseInfo;
import com.qf.data.course.entity.UserBuyRecored;
import com.qf.data.course.vo.input.CourseInfoInput;
import io.jsonwebtoken.lang.Collections;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * author Ken
 * create_time 2022/9/23
 */
@RestController
@RequestMapping("/course/info")
@Slf4j
public class CourseInfoController {

    @Autowired
    private CourseInfoService courseInfoService;

    @Autowired
    private CourseDescInfoService courseDescInfoService;

    @Autowired
    private UserBuyRecoredService userBuyRecoredService;

    /**
     * 根据教师id查询该教师名下的相关课程
     * @return
     */
    @RequestMapping("/listByTid")
    @GetUser
    public R listByTid(){
        BaseUser user = UserUtils.getUser();
        List<CourseInfo> courseInfos = courseInfoService.queryByTid(user.getId());
        return R.create(courseInfos);
    }

    /**
     * 新增课程的基本信息
     * @return
     */
    @RequestMapping("/insert")
    public R insert(CourseInfoInput courseInfoInput){
        log.debug("[course info insert] 新增课程信息 - {}", courseInfoInput);
        courseInfoService.save(courseInfoInput);
        return R.create("succ");
    }

    /**
     * 根据1级分类的id 查询该方法下所有的课程列表
     * @return
     */
    @RequestMapping("/queryByOneType")
    public R queryByOneType(Integer tid){
        return R.create(courseInfoService.queryByOneType(tid));
    }

    /**
     * 根据课程id 查询课程信息
     * @param cid
     * @return
     */
    @RequestMapping("/queryCourseById")
    public R queryCourseById(Integer cid){
        CourseInfo course = courseInfoService.queryCourseById(cid);
        log.debug("[query course] 根据课程id查询课程详情以及课程章节列表 - {}", course);
        return R.create(course);
    }

    /**
     * 查询课程下的子课程明细列表
     * @return
     */
    @RequestMapping("/querydesc")
    public R queryCourseDesc(Integer cid){
        log.debug("[query course desc] 查询课程明细 - {}", cid);
        List<CourseDescInfo> courseDescInfos = courseDescInfoService.query().eq("cid", cid).list();

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM-dd");

        Map<String, CourseDescInfo> descMap = courseDescInfos.stream()
                .collect(Collectors.toMap(courseDescInfo -> simpleDateFormat.format(courseDescInfo.getDescBeginTime()), courseDescInfo -> courseDescInfo));

        log.debug("[query course desc] 查询结果 - {}", descMap);
        return R.create(descMap);
    }

    /**
     * 添加课程章节
     * @return
     */
    @RequestMapping("/insertdesc")
    public R insertdesc(CourseDescInfo courseDescInfo){
        log.debug("[course desc insert] 添加课程章节...{}", courseDescInfo);
        courseDescInfoService.save(courseDescInfo);
        return R.create("succ");
    }

    /**
     *
     * @param cid
     * @return
     */
    @GetUser
    @RequestMapping("/buy")
    public R buyCourse(Integer cid) {
        BaseUser user = UserUtils.getUser();
        Integer id = user.getId();
        log.debug("[buy course] 购买课程 - {} - {}", id, cid);

        UserBuyRecored userBuyRecored = new UserBuyRecored()
                .setUid(id)
                .setCid(cid);

        userBuyRecoredService.save(userBuyRecored);

        return R.create(null);
    }

    /**
     * 查询我的课程列表
     * @return
     */
    @GetUser
    @RequestMapping("/queryMyCourse")
    public R queryMyCourse(){
        BaseUser user = UserUtils.getUser();
        Integer uid = user.getId();

        //查询当前用户的购买记录列表
        List<UserBuyRecored> recoreds = userBuyRecoredService.query().select("cid").eq("uid", uid).list();
        if (Collections.isEmpty(recoreds)) {
            return R.create(null);
        }

        List<CourseInfo> courseInfos = courseInfoService.query()
                .in("id", recoreds.stream().map(userBuyRecored -> userBuyRecored.getCid()).collect(Collectors.toList()))
                .orderByDesc("status")
                .orderByAsc("begin_time")
                .list();
        return R.create(courseInfos);
    }

    /**
     * 查询课程 中 可以开始上课的章节（半个小时以内的章节）
     * @param cid
     * @return
     */
    @RequestMapping("/querybegin")
    public R queryBeginCourseDesc(Integer cid){
        CourseDescInfo courseDescInfo = courseDescInfoService.queryBeginCourseDesc(cid);
        return R.create(courseDescInfo);
    }
}
